import 'dart:convert';

import 'package:common_utils/common_utils.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:sp_util/sp_util.dart';

/// showToast二次封装
///
///
toast(String message) {
  showToast(
    message,
    textPadding: EdgeInsets.symmetric(vertical: 7, horizontal: 10),
  );
}

Map<String, dynamic> buildParams({
  String method,
  dynamic params,
}) {
  print('进来的1');
  print(jsonEncode(params));
  var reqBase64 = EncryptUtil.encodeBase64(jsonEncode(params));
  // var reqBase64 ="eyJkYXRhX3NpZ24iOjAsImNoYW5uZWxfbnVtX2lkIjo5OCwidXNlX3BsYXRmb3JtIjozLCJtb2JpbGUiOiIxMzcwMTY5MzI3NSIsInNtc192ZXJpZnlfY29kZSI6IjY1NTE0Iiwic21zX3ZlcmlmeV90b2tlbiI6Ijk2YWExZThhLTU2ZmEtNGMyMS05OWFjLWUwMzllMWY3MDU5NiIsInNtc192ZXJpZnlfdHlwZSI6ImxvZ2luIn0=";
  var timestamp = DateUtil.formatDateStr(DateTime.now().toString(),
      format: "yyyyMMddHHmmss");
  String sign = "";

  /// cache
  ///
  var salt = SpUtil.getString('salt', defValue: '');
  var sid = SpUtil.getString('sid', defValue: '');

  if (sid.isNotEmpty && salt.isNotEmpty) {
    print('进来的1');
    String s = salt +
        "method" +
        method +
        "params" +
        reqBase64 +
        "sid" +
        sid +
        "timestamp" +
        timestamp +
        salt;
    var bytes = utf8.encode(s); // data being hashed
    sign = sha1.convert(bytes).toString().toLowerCase();
  } else {
    print('进来的2');
    String s = reqBase64 + "20001" + timestamp + method;
    // var bytes = utf8.encode(s); // data being hashed
    //sign = sha1.convert(bytes).toString().toLowerCase();
    sign = EncryptUtil.encodeMd5(s);
  }

  Map<String, dynamic> newParams = {
    "method": method,
    "app_key": "20001",
    "params": reqBase64,
    "timestamp": timestamp,
    "sign": sign,
    // "sid": sid,
  };
  return newParams;
}
